////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modif   //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "path.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <cstdarg>

#include "common.hpp"
#include "assert.hpp"

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

Path::Path() {
} // intentionally empty

Path::Path(const string& onlyString) {
	*this += onlyString;
}

Path::Path(const char* a1, ...) {
	va_list args;
	va_start(args, a1);
	for (const char * a = a1; a; a = va_arg(args, const char*))
		*this += string(a);
	va_end(args);
}

const string& Path::operator[](unsigned int index) const {
	assert(0 <= index && index < _path.size());
	
	return _path[index];
}

const string& Path::head() const {
	assert(depth() > 0);
	
	return _path[_path.size() - 1];
}

Path Path::tail() const {
	assert(depth() > 0);
	
	Path result;
	result._path.assign(_path.begin(), _path.end() - 1);
	return result;
}

unsigned int Path::depth() const {
	return _path.size();
}

const Path& Path::operator+=(const string& element) {
	assert(depth() == 0 || !element.empty());
	
	_path.push_back(element);
	return *this;
}

bool Path::operator<(const Path& other) const {
	return (_path < other._path);
}

bool Path::operator==(const Path& other) const {
	return (_path == other._path);
}

bool Path::is_absolute() const {
	assert(depth() > 0);
	
	return _path[0].empty();
}

ostream& Path::report_to_stream(ostream& stream) const {
	return stream << implode(_path, ".");
}

ostream& operator<<(ostream& stream, const Path& path) {
	return path.report_to_stream(stream);
}

Path::iterator Path::begin() {
	return _path.begin();
}

Path::iterator Path::end() {
	return _path.end();
}

Path::const_iterator Path::begin() const {
	return _path.begin();
}

Path::const_iterator Path::end() const {
	return _path.end();
}
